package com.whfc.emp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.emp.dao.AppEmpContractAttachMapper;
import com.whfc.emp.dao.AppEmpContractMapper;
import com.whfc.emp.dto.AppAttachDTO;
import com.whfc.emp.dto.AppEmpContractDTO;
import com.whfc.emp.entity.AppEmpContract;
import com.whfc.emp.entity.AppEmpContractAttach;
import com.whfc.emp.param.AppContractAddParam;
import com.whfc.emp.param.AppContractEditParam;
import com.whfc.emp.service.AppContractService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClasssName AppContractServiceImpl
 * @Description 人员合同
 * @Author hw
 * @Date 2020/12/30 15:48
 * @Version 1.0
 */
@DubboService(interfaceClass = AppContractService.class, version = "1.0.0")
public class AppContractServiceImpl implements AppContractService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppEmpContractMapper appEmpContractMapper;

    @Autowired
    private AppEmpContractAttachMapper appEmpContractAttachMapper;

    @Override
    public List<AppEmpContractDTO> list(Integer empId) {
        logger.info("合同列表:empId:{}", empId);
        List<AppEmpContractDTO> list = appEmpContractMapper.selectByEmpId(empId);
        for (AppEmpContractDTO contractDTO : list) {
            Integer id = contractDTO.getId();
            List<AppAttachDTO> attachList = appEmpContractAttachMapper.selectByContractId(id);
            contractDTO.setFileAttachList(attachList);
            contractDTO.setFileNo(attachList.size());
        }
        return list;
    }

    @Override
    public void add(AppContractAddParam param) {
        logger.info("添加合同,param:{}", param);
        // 根据合同编号去重
        String contractNo = param.getContractNo();
        AppEmpContract appEmpContract = appEmpContractMapper.selectByContractNo(contractNo);
        if (appEmpContract != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该合同编号已存在");
        }

        // 插入合同
        AppEmpContract record = new AppEmpContract();
        BeanUtils.copyProperties(param, record);
        appEmpContractMapper.insertSelective(record);
        Integer id = record.getId();

        // 插入合同附件
        List<AppAttachDTO> fileAttachList = param.getFileAttachList();
        if (fileAttachList != null && fileAttachList.size() > 0) {
            List<AppEmpContractAttach> list = new ArrayList();
            for (AppAttachDTO attachDTO : fileAttachList) {
                AppEmpContractAttach attach = new AppEmpContractAttach();
                attach.setContractId(id);
                attach.setName(attachDTO.getName());
                attach.setUrl(attachDTO.getUrl());
                list.add(attach);
            }
            appEmpContractAttachMapper.batchInsert(list);
        }
    }

    @Override
    public void edit(AppContractEditParam param) {
        logger.info("添加合同,编辑合同:{}", JSONObject.toJSONString(param));
        Integer contractId = param.getId();
        // 根据合同编号去重
        String contractNo = param.getContractNo();
        AppEmpContract appEmpContract = appEmpContractMapper.selectByContractNo(contractNo);
        if (appEmpContract != null && !appEmpContract.getId().equals(contractId)) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该合同编号已存在，不能重复");
        }
        // 编辑合同
        AppEmpContract record = new AppEmpContract();
        BeanUtils.copyProperties(param, record);
        appEmpContractMapper.updateByPrimaryKeySelective(record);

        // 插入合同附件
        appEmpContractAttachMapper.deleteLogicByContractId(contractId);
        List<AppAttachDTO> fileAttachList = param.getFileAttachList();
        if (fileAttachList != null && fileAttachList.size() > 0) {
            List<AppEmpContractAttach> list = new ArrayList();
            for (AppAttachDTO attachDTO : fileAttachList) {
                AppEmpContractAttach attach = new AppEmpContractAttach();
                attach.setName(attachDTO.getName());
                attach.setContractId(contractId);
                attach.setUrl(attachDTO.getUrl());
                list.add(attach);
            }
            appEmpContractAttachMapper.batchInsert(list);
        }
    }

    @Override
    public void del(Integer id) {
        logger.info("删除合同:id:{}", id);
        appEmpContractMapper.deleteLogicById(id);
    }

    @Override
    public List<AppAttachDTO> selectByEmpIdUrl(Integer empId) {
        return appEmpContractMapper.selectByEmpIdUrl(empId);
    }
}
